<?php
/**
* @package Frontend-User-Access (com_frontenduseraccess)
* @version 3.0.8
* @copyright Copyright (C) 2008 Carsten Engel. All rights reserved.
* @license GPL versions free/trial/pro
* @author http://www.pages-and-items.com
* @joomla Joomla is Free Software
*/

// no direct access
defined('_JEXEC') or die('Restricted access');

if(!$this->controller->fua_config['display_categories'] && $this->controller->user_type!='Super Administrator'){
	die('Restricted access');
}

//header and nav
$this->controller->echo_header();

//make javascript array from sections
$javascript_array_categories = 'var categories = new Array(';
$first = true;
foreach($this->categories as $category){		
	if($first){
		$first = false;
	}else{
		$javascript_array_categories .= ',';
	}
	$javascript_array_categories .= "'".$category->id."'";
}	
$javascript_array_categories .= ');';
		
?>
<script language="javascript" type="text/javascript">

<?php echo $javascript_array_categories."\n"; ?>

function select_all(usergroup_id, select_all_id){
	action = document.getElementById(select_all_id).checked;	
	for (i = 0; i < categories.length; i++){
		box_id = categories[i]+'__'+usergroup_id;
		hidden_id = categories[i]+'__'+usergroup_id+'__hidden';
		if(action==true){
			document.getElementById(box_id).checked = true;
			document.getElementById(hidden_id).value = hidden_id+'__1';
		}else{
			document.getElementById(box_id).checked = false;
			document.getElementById(hidden_id).value = hidden_id+'__';
		}
	}	
}

function toggle_right(hidden_field_id){
	field = document.getElementById(hidden_field_id);
	if(field.value==hidden_field_id+'__1'){
		field.value = hidden_field_id+'__';
	}else{
		field.value = hidden_field_id+'__1';
	}
}

function check_limitstart(){
	ori_search = '<?php echo $this->search;?>';	
	new_search = document.getElementById('search').value;	
	if(ori_search!=new_search){
		document.adminForm.limitstart_needs_reset.value = 1;
	}else{
		document.adminForm.limitstart_needs_reset.value = 0;
	}	
	return true;
}

</script>
<form name="adminForm" method="post" action="">
	<input type="hidden" name="option" value="com_frontenduseraccess" />
	<input type="hidden" name="task" value="" />
	<input type="hidden" name="limitstart_needs_reset" value="" />	
	<?php echo JHTML::_( 'form.token' ); ?>	
<table id="fua_subheader">
	<tr>
		<td>
			<?php echo '<p>'.JText::_('CATEGORIES_INFO').'.</p>';

			//legend and message if reverse access	
			$this->controller->reverse_access_warning('category_reverse_access');
			
			//message in free version that these restrictions will not work in free version
			$this->controller->not_in_free_version();
			
			//message if category access is not activated	
			if($this->controller->fua_config['categories_active']==false){				
				echo '<div style="color: red; text-align: left;">'.JText::_('NO_CATEGORIES_ACTIVE').'. <a href="index2.php?option=com_frontenduseraccess&view=config&tab=category_access">'.JText::_('ACTIVATE_IN_CONFIG').'</a><br/><br/></div>';
			}	
			
			echo JText::_('FILTER'); ?>:
			<input type="text" name="search" id="search" value="<?php echo $this->search;?>" class="text_area"  onchange="check_limitstart();" />
			<button onclick="check_limitstart();this.form.submit();"><?php echo JText::_('GO'); ?></button>
			<button onclick="document.getElementById('search').value='';this.form.submit();"><?php echo JText::_('RESET'); ?></button>
			<?php
			
			$selected = 'selected="selected"';					
			
			echo '&nbsp;&nbsp;&nbsp;<select name="section_filter" id="section_filter" onchange="check_limitstart();this.form.submit();">';
			echo '<option value="">'.JText::_('SELECT SECTION').'</option>';
			foreach($this->sections as $section){		
				echo '<option value="'.$section->id.'"';
				if($section->id==$this->section_filter){
					echo $selected;
				}				
				echo '>'.$section->title.'</option>';						
			}
			echo '</select>';
			
			?>
		</td>
		<td id="td_usergroup_selector">
			<?php echo $this->controller->usergroup_selector(); ?>
		</td>
	</tr>
</table>		
<table class="adminlist">
	<tr>		
		<th align="left">&nbsp;
						
		</th>
		<?php			
			$this->controller->loop_usergroups($this->fua_usergroups);			
		?>		
		
	</tr>
		
	<?php
							
		$k = 1;		
		
		//row with select_all checkboxes
		echo '<tr class="row1">';
		echo '<td>'.JText::_('SELECTALL').'</td>';
		foreach($this->fua_usergroups as $fua_usergroup){
			echo '<td style="text-align:center;"><input type="checkbox" name="checkall[]" value="" id="checkall_'.$fua_usergroup->id.'" onclick="select_all('.$fua_usergroup->id.',this.id);" /></td>';
		}
		echo '</tr>';
		
		$counter = 0;	
		foreach($this->categories as $category){	
							
			echo '<tr class="row'.$k.'"><td>';
			if($category->title!=''){
				echo $category->title;
			}else{
				echo $category->name;
			}			
			echo '</td>';			
			foreach($this->fua_usergroups as $fua_usergroup){
				$checked = '';
				$checked_hidden = '';
				if (in_array($category->id.'__'.$fua_usergroup->id, $this->access_categories)) {
					$checked = 'checked="checked"';
					$checked_hidden = '1';
				}
				echo '<td style="text-align:center;"><input type="hidden" name="category_access_hidden[]" id="'.$category->id.'__'.$fua_usergroup->id.'__hidden" value="'.$category->id.'__'.$fua_usergroup->id.'__hidden__'.$checked_hidden.'" /><input type="checkbox" name="category_access[]" id="'.$category->id.'__'.$fua_usergroup->id.'" onclick="toggle_right(\''.$category->id.'__'.$fua_usergroup->id.'__hidden\');" value="'.$category->id.'__'.$fua_usergroup->id.'" '.$checked.' /></td>';
			}
			echo '</tr>';
			if($k==1){
				$k = 0;
			}else{
				$k = 1;
			}
			if($counter==7){
				echo '<tr><th>&nbsp;</th>';	
				$this->controller->loop_usergroups($this->fua_usergroups);
				echo '</tr>';
				$counter = 0;
			}
			$counter = $counter+1;						
		}	
	echo '<table class="adminlist"><tfoot><tr><td>'.$this->pagination->getListFooter().'</td></tr></tfoot></table>';
	?>			
</table>
</form>
<?php

$this->controller->display_footer();

?>